\subsection{Serialising Data}
\label{implementation:software:serializing}
The individual frames are stored in buffers. This is not suitable for the \ac{RLNC} encoder therefore the entire \ac{GOP} is serialized into one buffer. The serialised buffer is zero padded enough to fit the generation size and symbol size. The format is shown in Figure \ref{fig:serialized_buffer}. The source code is available in Appendix \ref{source_code:serializer}. Figure \ref{fig:serialize} shows a flow chart for the serialiser.

\begin{figure}[h!]
\centering
	\begin{tikzpicture}[>=stealth',shorten >=1pt,auto, semithick]
	\tikzstyle{every state}=[rectangle, fill=white,text=black,,minimum height=1.2cm, minimum width=1.6cm, node distance=1.6cm,align=center,font=\footnotesize]
		\node[state] (L2) {Size of\\ buffer 1};
		\node[state, right of=L2] (L3) {Buffer 1};
		\node[state, right of=L3] (L4) {Size of\\ buffer 2};
		\node[state, right of=L4] (L5) {Buffer 2};
		\node[state, right of=L5] (L6) {Size of\\ buffer 3};
		\node[state, right of=L6] (L7) {Buffer 3};
		\node[state, right of=L7] (L8) {Size of\\ buffer N};
		\node[state, right of=L8] (L9) {Buffer N};
	\end{tikzpicture}
\caption{The frames are stacked next to each other only separated by a size indicator, thus the data is serialised.}
\label{fig:serialized_buffer}
\end{figure}

%\fxnote{E: Wait unit? -> until figure 4,8. Also i would like a clear starting point indicator. Could we elaborate on what a ffmpeg packet is in the fig caption?}

\begin{figure}[h!]
\centering
	\begin{tikzpicture}[>=stealth',auto, semithick]

	\tikzstyle{every state}=[rectangle, fill=white,text=black,minimum height=1.2cm, minimum width=2.1cm, node distance=2.7cm,align=center, font=\tiny]

		\node[state] (1) {Wait until new\\ packet is received\\ from FFmpeg};
		\node[state, right of=1] (2) {Allocate new\\ memory for\\ packet size};
		\node[state, right of=2] (3) {Append packet\\ size to serial\\ buffer};
		\node[state, right of=3] (4)  {Allocate new\\ memory for\\ packet data};
		\node[state, right of=4] (5) {Append packet\\ data to serial\\ buffer};
		\node[state, right of=5] (6) {Return\\ serialised\\ buffer};

		\path	(1) edge[->,shorten >=1pt] (2);
		\path	(2) edge[->,shorten >=1pt] (3);
		\path	(3) edge[->,shorten >=1pt] (4);
		\path	(4) edge[->,shorten >=1pt] (5);
		\path	(5) edge[->,shorten >=1pt] node[above, yshift=0.6cm, font=\tiny] {If GOP is complete} (6);
		\draw[-] (5.south) -- ([yshift=-0.5cm]5.south);
		\draw[-] ([yshift=-0.5cm]5.south) -- node[below,font=\tiny] {If GOP is not complete} ([yshift=-0.5cm]1.south);
		\draw[->,shorten >=1pt] ([yshift=-0.5cm]1.south) -- (1.south);
		
	\end{tikzpicture}
\caption{Serialising a generation, GOP. The packet received from FFmpeg consists of a buffer containing a single compressed video frame. The initial (and main) state of the serialisation process is the waiting-state.}
\label{fig:serialize}
\end{figure}

\newpage
When the decoded data reaches the deserialiser the last part of the data may be zero padded. The deserialiser handles this by simply stopping if the next length is zero, which avoids unnecessary inspection of the remaining data. If no zero padding is required the deserialiser stops at the end. Listing \ref{lst:deserialize} shows pseudo code for the deserialiser.

\noindent\minipage{\textwidth}
\begin{lstlisting}[language=C,caption=Deserialising a generation.,label={lst:deserialize},captionpos=b]
Index = 0
Next buffer size = 0
while index < length of serialised data
	Next buffer size = serialised data[index]
	if Next buffer size + index > length of serialised data
		break
	if Next buffer size is zero
		break
	index = index + size of index
	copy from index, to FFmpeg buffer, with size: Next buffer size
	index = index + Next buffer size
\end{lstlisting}
\endminipage
